package www.dianbaozu.com.ui.my

import android.os.Bundle
import android.view.View
import androidx.annotation.NonNull
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshLayout
import dagger.hilt.android.AndroidEntryPoint
import www.dianbaozu.com.R
import www.dianbaozu.com.databinding.FragmentHelpCenterBinding
import www.dianbaozu.com.extension.requestUniversally
import www.wen.com.common.lib.base.fragment.BaseListFragment
import www.wen.com.common.lib.base.module.BaseViewModel
import www.wen.com.common.lib.data.entities.ContractEntity
import www.wen.com.common.lib.data.entities.ProtocolEntity
import www.wen.com.common.lib.data.entities.ViewPagePosition
import www.wen.com.common.lib.extension.nav
import www.wen.com.common.lib.extension.popProxy
import www.wen.com.common.lib.extension.postObject
import www.wen.com.common.lib.helper.loading.KeyHelper
import www.wen.com.common.lib.helper.loading.TitleBarUniversalAdapter

/**
 * @author WEN
 * @Description: 帮助中心
 * @date 2020//25 17:48
 */
@AndroidEntryPoint
class HelpCenterFragment :
        BaseListFragment<FragmentHelpCenterBinding, ProtocolEntity, ProtocolEntity.Content>() {

    override val loadMoreEnd = true

    private lateinit var mAdapter: IssueListAdapter

    private val mBaseApiViewModel: HelpCenterViewModel by viewModels()

    override val viewModel: BaseViewModel
        get() = mBaseApiViewModel

    override val mRecyclerView: RecyclerView
        get() = dataBinding.rvIssue

    override val mSmartRefreshLayout: SmartRefreshLayout
        get() = dataBinding.smartRefreshLayout

    override fun provideAdapter(): BaseQuickAdapter<ProtocolEntity.Content, BaseViewHolder> =
            IssueListAdapter().apply {
                mAdapter = this
            }

    override fun isEnableLoadingHelper(): Boolean = true

    override fun getTitleBarUniversalAdapter(): TitleBarUniversalAdapter =
            TitleBarUniversalAdapter(requireActivity(), "", action = ::titleAction)

    override fun getLayoutId(): Int = R.layout.fragment_help_center

    override fun lazyInitView() {
        super.lazyInitView()
        launchRequest(false)
        observeListen()
    }

    private fun observeListen() {
        mBaseApiViewModel.uiState.observe(this) {
            if (it.showSuccess != null) {
                if (it.isRefresh) {
                    mAdapter.setList(it.showSuccess)
                } else {
                    mAdapter.addData(it.showSuccess!!)
                }
            }
        }
    }

    override fun onRefresh(@NonNull refreshLayout: RefreshLayout) {
        launchRequest(true)
    }

    override fun onLoadMore() {
        launchRequest(false)
    }

    private fun launchRequest(isRefresh: Boolean) {
        mBaseApiViewModel.requestGeneralListData("", isRefresh = isRefresh)
    }

    override fun onReload() {
        launchRequest(false)
    }

    override fun onClick(v: View) {
        mBaseApiViewModel.requestUniversally(
                viewModelScope = lifecycleScope,
                tipSuccess = "",
                isShowToastTip = false,
                request = { appDataManage -> appDataManage.requestServerOfficial() }
        ) { serverInfo->
            if(serverInfo.serverMember!=null){
                //已经加入服务器则返回首页选中官方服务器
                postObject(KeyHelper.REFRESHING_SERVER_LIST_KEY2, serverInfo)
                //首页切换到-服务器
                postObject(KeyHelper.MAIN_VIEWPAGER_POSITION_KEY, ViewPagePosition(0))
                //返回首页
                popProxy(R.id.mainFragment)
            }else{
                //把官方服务器当做预览服务器插入 首页-服务器
                postObject(KeyHelper.INSERT_PREVIEW_SERVER_KEY, serverInfo)
                //首页切换到-服务器
                postObject(KeyHelper.MAIN_VIEWPAGER_POSITION_KEY, ViewPagePosition(0))
                //返回首页
                popProxy(R.id.mainFragment)
            }
        }
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val issue = adapter.data[position] as ProtocolEntity.Content
        nav(R.id.action_helpCenterFragment_to_contractFragment, args = Bundle().apply {
            putParcelable(KeyHelper.BUNDLE_DATA, ContractEntity(issue.name, "帮助中心",issue.content))
        })
    }

    internal class IssueListAdapter : BaseQuickAdapter<ProtocolEntity.Content, BaseViewHolder>(R.layout.item_help),
            LoadMoreModule {

        override fun convert(holder: BaseViewHolder, item: ProtocolEntity.Content) {
            holder.setText(R.id.tvIssue, item.name)
        }

    }

}



